package cn.ecut.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ecut.Dao.UserDao;
import cn.ecut.Bean.User;

@WebServlet("/jsp/LoginServlet")
public class LoginServlet extends HttpServlet{
    public LoginServlet() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao ud = new UserDao();
        User user = ud.userLogin(name, password);
        if(user != null){
            request.getRequestDispatcher("/jsp/success.jsp").forward(request, response);
        }else{
            response.sendRedirect("userlogin.jsp?error=yes");
        }
    }

}
